package com.demo.modules.image.service.impl;

import com.alibaba.cola.extension.BizScenario;
import com.alibaba.cola.extension.ExtensionExecutor;
import com.alibaba.fastjson.JSON;
import com.demo.constants.AiPlatformConstants;
import com.demo.modules.base.BaseAiConfigDto;
import com.demo.modules.facade.ImageContentParseFacade;
import com.demo.modules.image.domain.dto.ImageContentUrlDto;
import com.demo.modules.image.extension.ImageContentExtPtService;
import com.demo.modules.image.service.ImageContentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 图片内容解析
 *
 * @author admin
 */
@Service("imageContentService")
public class ImageContentServiceImpl implements ImageContentService {
    private static final Logger log = LoggerFactory.getLogger(ImageContentServiceImpl.class);

    @Value("${aliyun.appSecret}")
    private String appSecret;

    @Value("${aliyun.prompt}")
    private String prompt;

    @Value("${aliyun.model}")
    private String model;

    @Resource
    private ExtensionExecutor extensionExecutor;

    /**
     * 根据图片url解析图片内容
     *
     * @param imageContentUrlDto 图片内容解析
     * @return String
     */
    @Override
    public String parseForUrl(ImageContentUrlDto imageContentUrlDto) {
        try {
            BaseAiConfigDto baseAiConfigDto = new BaseAiConfigDto();
            baseAiConfigDto.setModel(model);
            baseAiConfigDto.setPrompt(prompt);
            baseAiConfigDto.setAppSecret(appSecret);
            BizScenario bizScenario = BizScenario.valueOf(AiPlatformConstants.ALI_YUN);
            return extensionExecutor.execute(ImageContentExtPtService.class, bizScenario,
                    extension -> extension.parseImageContent(imageContentUrlDto.getImageUrl(), baseAiConfigDto));
        } catch (Exception e) {
            log.error("图片解析失败", e);
            return null;
        }
    }
}
